package com.ngm.tea.tea.service.impl;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import com.ngm.tea.common.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.tea.dao.SellerDao;
import com.ngm.tea.tea.entity.Seller;
import com.ngm.tea.tea.service.SellerService;

/**
 * 商家服务层
 * @author zjw
 * @version 1.0
 * @date 2017-01-03 15:51:34
 *
 */
@Service("sellerService")
public class SellerServiceImpl implements SellerService {
	
	@Resource
	private SellerDao sellerDao;

	public SellerDao getSellerDao() {
		return sellerDao;
	}

	@Override
	public void insert(Seller seller) {
		sellerDao.insert(seller);
	}

	@Override
	public void deleteById(Long id) {
		sellerDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			sellerDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(Seller seller) {
		sellerDao.delete(seller);
	}

	@Override
	public void update(Seller seller) {
		sellerDao.update(seller);
	}

	@Override
	public Seller selectOne(Long id) {
		return sellerDao.selectOne(id);
	}

	@Override
	public List<Seller> selectAll() {
		return sellerDao.selectAll();
	}

	@Override
	public List<Seller> selectList(Seller seller, PageInfo pageInfo) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(seller));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return sellerDao.selectList(map);
	}

	@Override
	public Long count(Seller seller) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(seller));
		return sellerDao.count(map);
	}
	
	public String makeStr(Seller seller) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtils.isEmpty(seller.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + seller.getId()+"%'");
			} else {
				sb.append(" AND id LIKE '%" + seller.getId()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getServiceScore())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("serviceScore LIKE '%" + seller.getServiceScore()+"%'");
			} else {
				sb.append(" AND serviceScore LIKE '%" + seller.getServiceScore()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getRankRate())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("rankRate LIKE '%" + seller.getRankRate()+"%'");
			} else {
				sb.append(" AND rankRate LIKE '%" + seller.getRankRate()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getRatingCount())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("ratingCount LIKE '%" + seller.getRatingCount()+"%'");
			} else {
				sb.append(" AND ratingCount LIKE '%" + seller.getRatingCount()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getScore())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("score LIKE '%" + seller.getScore()+"%'");
			} else {
				sb.append(" AND score LIKE '%" + seller.getScore()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getAvatar())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("avatar LIKE '%" + seller.getAvatar()+"%'");
			} else {
				sb.append(" AND avatar LIKE '%" + seller.getAvatar()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getPics())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("pics LIKE '%" + seller.getPics()+"%'");
			} else {
				sb.append(" AND pics LIKE '%" + seller.getPics()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getInfos())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("infos LIKE '%" + seller.getInfos()+"%'");
			} else {
				sb.append(" AND infos LIKE '%" + seller.getInfos()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getFoodScore())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("foodScore LIKE '%" + seller.getFoodScore()+"%'");
			} else {
				sb.append(" AND foodScore LIKE '%" + seller.getFoodScore()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getDeliveryTime())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("deliveryTime LIKE '%" + seller.getDeliveryTime()+"%'");
			} else {
				sb.append(" AND deliveryTime LIKE '%" + seller.getDeliveryTime()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getDeliveryPrice())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("deliveryPrice LIKE '%" + seller.getDeliveryPrice()+"%'");
			} else {
				sb.append(" AND deliveryPrice LIKE '%" + seller.getDeliveryPrice()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getDescription())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("description LIKE '%" + seller.getDescription()+"%'");
			} else {
				sb.append(" AND description LIKE '%" + seller.getDescription()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getName())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("name LIKE '%" + seller.getName()+"%'");
			} else {
				sb.append(" AND name LIKE '%" + seller.getName()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getBulletin())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("bulletin LIKE '%" + seller.getBulletin()+"%'");
			} else {
				sb.append(" AND bulletin LIKE '%" + seller.getBulletin()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getSellCount())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("sellCount LIKE '%" + seller.getSellCount()+"%'");
			} else {
				sb.append(" AND sellCount LIKE '%" + seller.getSellCount()+"%'");
			}
		}
		if(!StringUtils.isEmpty(seller.getMinPrice())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("minPrice LIKE '%" + seller.getMinPrice()+"%'");
			} else {
				sb.append(" AND minPrice LIKE '%" + seller.getMinPrice()+"%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}
	
}